﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdatePro
{
    public partial class FrmUpdate : Form
    {
        private UpdateManager updateManager = new UpdateManager();
        public FrmUpdate()
        {
            InitializeComponent();
            //初始化更新页面
            Init();

        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            //1.禁用相关的按钮
            this.btnFinish.Enabled = false;
            this.btnCancel.Enabled = false;
            //2.将同步显示进的的方法与委托关联
            updateManager.showUpdateProcess = ShowPrecent;
            //3.展示数据
            this.lblVersion.Text = updateManager.LastUpdateInfo.Version;
            this.lblLastUpdateTime.Text = updateManager.LastUpdateInfo.UpdateTime.ToString();
            foreach (var item in updateManager.NowUpdateInfo.FileList)
            {
                this.lvUpdateList.Items.Add(new ListViewItem(item));
            }
        }

        /// <summary>
        /// 根据委托定义同步显示下载百分比
        /// </summary>
        /// <param name="fileIndex"></param>
        /// <param name="finishedPrecent"></param>
        public void ShowPrecent(int fileIndex, int finishedPrecent)
        {
            this.lvUpdateList.Items[fileIndex].SubItems[3].Text = finishedPrecent+ "%";
            //进度条的展示
            this.pbDownLoadFile.Maximum = 100;
            this.pbDownLoadFile.Value = finishedPrecent;
            if (fileIndex < this.lvUpdateList.Items.Count-1 && finishedPrecent == 100)
            {
                this.pbDownLoadFile.Value = 0;
            }
        }

        #region 窗体移动代码
        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//鼠标是否为左键
        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y);//获取移动变量的值
                leftFlag = true;//点击左键按下时标注为true
            }
        }

        private void Frm_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后leftFalg为false
            }
        }

        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);//设置移动的位置
                Location = mouseSet;
            }
        }
        #endregion
        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认取消升级吗", "询问提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //关闭升级程序
                Application.ExitThread();
                Application.Exit();
            }
        }

        //完成
        private void btnFinish_Click(object sender, EventArgs e)
        {
            try
            {
                if (updateManager.CopyFile())//调用复制
                {

                    //启动主程序
                    Process.Start("Library.exe");
                    //关闭当前程序
                    Application.ExitThread();
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("升级异常：" + ex.Message);
            }
        }
        //下一步
        private void btnNext_Click(object sender, EventArgs e)
        {
            //下载文件
            this.btnNext.Enabled = false;
            this.btnCancel.Enabled = true; 
            try
            {
                this.lblUpdateStatus.Text = "正在下载更新文件，请稍后...";
                this.lblTips.Text = "点击'取消'可以结束升级...";
                updateManager.DownLoadFiles();
                this.lblUpdateStatus.Visible = false;
                this.lblTips.Text = "全部文件已经下载，点击'完成'完成升级";
                //this.btnNext.Enabled = false;
                this.btnCancel.Visible = false;
                this.btnNext.Visible = false;
                this.btnFinish.Enabled = true;
                this.btnFinish.Location = this.btnCancel.Location;
            }
            catch (Exception ex)
            {
                this.btnNext.Enabled = true;
                this.btnCancel.Enabled = false;
                MessageBox.Show("升级异常："+ex.Message);
            }
        }
    }
}
